package cn.edu.zjicm.wordsnet_d.data.test.selection;

import java.util.LinkedList;
import java.util.List;


import android.content.Context;
import android.widget.TextView;

import cn.edu.zjicm.wordsnet_d.data.Affix;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.util.StringUtil;

public class AffixTestOld{
	protected List<Integer> used = new LinkedList<Integer>();
	WordFactory wf;	
	Context mContext;
	String promopt = null;
	OneWord theWord;
	Affix affix;
	
	public AffixTestOld(Context mContext){
		this.mContext = mContext;
		this.wf = WordFactory.getInstance(mContext);;
	}
	
	public String getQuestion(OneWord theWord) {
		// TODO Auto-generated method stub
		this.theWord = theWord;
		//List<Affix> affixList = wf.getAffixList(theWord.getIndex_word_id());
		
		affix = wf.getWordRoot(theWord.getIndex_word_id());			
		
		used.clear();
		used.add(affix.getAffix_id());
		
		int index = wf.getAffixNodeIndex(theWord.getIndex_word_id(), affix.getAffix_id());
		return StringUtil.hideAffixSense(theWord.getAff_content(), index);
		//return WordFactory.hideAffixSense(theWord.getAff_content(), affixList, affix.getAffix_id());
		//return WordFactory.hideAffixSense(theWord.getAff_content(), affix.getAffix());
	}
	
	public String getRightChoice(OneWord theWord) {
		// TODO Auto-generated method stub
		return affix.getAff_sense();
	}

	public void promopt(TextView tv) {
		// TODO Auto-generated method stub
		tv.setText(promopt);
	}
	
	public String getWrongChoice(OneWord theWord) {
		// TODO Auto-generated method stub
		String where = " aff_id!=" + affix.getAffix_id();
		int total = wf.getInt("select count(1) from affix_info where " + where);

		Affix wrong = null;
		for (int i = 0; i < 100 // 似乎有重复记录，避免死循环
				        && (wrong == null || used.contains(wrong.getAffix_id())); i++) {
			wrong = wf.getOneAffixOfWhere(where + " order by aff_id limit "
					+ WordFactory.rand(total) + ",1");
		}
		used.add(wrong.getAffix_id());

		return wrong.getAff_sense();
	}
}
